home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / MacWT 0.9 / wt Source / graphfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-10  |  3.5 KB  |  164 lines  |  [TEXT/CWIE]

  1. /*
  2. **  MacWT -- a 3d game engine for the Macintosh
  3. **  © 1995, Bill Hayden and Nikol Software
  4. **  Free for non-commercial use - address questions to the e-mail address below
  5. **
  6. **  Mail:           afn28988@freenet.ufl.edu (Bill Hayden)
  7. **    MacWT FTP site: ftp.circa.ufl.edu/pub/software/ufmug/mirrors/LocalSW/Hayden/
  8. **  WWW Page:       http://grove.ufl.edu:80/~nikolsw
  9. **
  10. **    All of the above addresses are due to changes sometime in 1996, so stay tuned
  11. **
  12. **  based on wt, by Chris Laurel
  13. **
  14. **  This program is distributed in the hope that it will be useful,
  15. **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. */
  18.  
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include "wt.h"
  24. #include "error.h"
  25. #include "wtmem.h"
  26. #include "color.h"
  27. #include "graphfile.h"
  28.  
  29.  
  30. /* MAGIC_LENGTH must be the length of the longest MAGIC string. */
  31. #define MAGIC_LENGTH 5
  32. #define GIF_MAGIC "GIF87"
  33. #define GIF89_MAGIC "GIF89"
  34.  
  35.  
  36. extern Graphic_file *LoadGIF(FILE *fp, char *filename);
  37.  
  38. static Graphic_file_format check_format(FILE *fp, char *filename);
  39.  
  40.  
  41. Graphic_file *new_graphic_file()
  42. {
  43.     Graphic_file *gf = (Graphic_file *)wtmalloc(sizeof(Graphic_file));
  44.  
  45.     gf->transparent_entry = -1;
  46.  
  47.     return gf;
  48. }
  49.  
  50.  
  51. void free_graphic_file(Graphic_file *gfile)
  52. {
  53.     if (gfile != NULL)
  54.         {
  55.         if (gfile->palette != NULL)
  56.             wtfree(gfile->palette);
  57.         if (gfile->bitmap != NULL)
  58.             wtfree(gfile->bitmap);
  59.         }
  60.         
  61.     wtfree(gfile);
  62. }
  63.  
  64.  
  65. /* Return false if the pixel is transparent, true if it is opaque.  In any
  66. **   case, stuff dest with the pixel RGB value.
  67. */
  68. Boolean graphic_file_pixel(Graphic_file *gfile, short x, short y, RGBcolor *rgb)
  69. {
  70.     if (gfile->type == gfPaletted)
  71.         {
  72.         int index;
  73.  
  74.         index = gfile->bitmap[y * gfile->width + x];
  75.         if (index == gfile->transparent_entry)
  76.             return FALSE;
  77.         else
  78.             {
  79.             *rgb = gfile->palette[index];
  80.             return TRUE;
  81.             }
  82.         }
  83.     else
  84.         {
  85.         unsigned char *pixel = gfile->bitmap + (y * gfile->width + x) * 3;
  86.  
  87.         rgb->red = *pixel++;
  88.         rgb->green = *pixel++;
  89.         rgb->blue = *pixel;
  90.  
  91.       /* For now, let pure cyan be the transparent color for true color images. */
  92.         if (rgb->red == 0 && rgb->green == 255 && rgb->blue == 255)
  93.             return FALSE;
  94.         else
  95.             return TRUE;
  96.         }
  97. }
  98.  
  99.       
  100. Graphic_file *read_graphic_file(char *filename)
  101. {
  102.     FILE *fp;
  103.     Graphic_file_format format;
  104.     Graphic_file *gfile = NULL;
  105.  
  106.  
  107.     if ((fp = fopen(filename, "rb")) == NULL)
  108.         fatal_error("Could not open texture %s", filename);
  109.  
  110.     format = check_format(fp, filename);
  111.     rewind(fp);
  112.  
  113.     switch (format)
  114.         {
  115.         case formatGIF89:
  116.         case formatGIF87:
  117.             gfile = LoadGIF(fp, filename);
  118.             break;
  119.  
  120.         case formatPICTR:
  121.             gfile = LoadPICTR(fp, filename);
  122.             break;
  123.  
  124.         case formatUnknown:
  125.             fatal_error("Unknown graphic file format.\n");
  126.             break;
  127.  
  128.         default:
  129.             fatal_error("The graphic file reading code is really broken.\n");
  130.             break;
  131.         }
  132.  
  133.     if (gfile == NULL)
  134.         fatal_error("Error reading texture %s\n");
  135.  
  136.     fclose(fp);
  137.  
  138.     return gfile;
  139. }
  140.       
  141.  
  142. static Graphic_file_format check_format(FILE *fp, char *filename)
  143. {
  144.     char magic[MAGIC_LENGTH];
  145.  
  146. #if __MWERKS__ || __THINKC__
  147.     // to read PICT or PICTR files, which are further along on the
  148.     // evolutionary chain than to need MagicCookies or the like.
  149.     fread(magic, 1, MAGIC_LENGTH, fp);
  150. #else
  151.     if (fread(magic, 1, MAGIC_LENGTH, fp) != MAGIC_LENGTH)
  152.         fatal_error("Error reading texture %s.", filename);
  153. #endif
  154.  
  155.     if (strncmp(magic, GIF_MAGIC, sizeof(GIF_MAGIC) - 1) == 0)
  156.         return formatGIF87;
  157.     else if (strncmp(magic, GIF89_MAGIC, sizeof(GIF89_MAGIC) - 1) == 0)
  158.         return formatGIF89;
  159.     else
  160.         return formatPICTR;
  161.         
  162.     // return formatUnknown;
  163. }
  164.